package user;

import utils.JsonOut;
import utils.MysqlUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebServlet(name = "SetUserInfoServlet")
public class SetUserInfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String email = UserSession.getUserSessionEmail(session);
        PrintWriter out = response.getWriter();
        if (email == null || email.isEmpty()){
            out.println(JsonOut.error(220, "not_login"));
            return;
        }

        //nickname,area,sex,birthday,head_image
        String nickname = request.getParameter("nickname");
        String signature = request.getParameter("signature");
        String area = request.getParameter("area");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
//        String headImage = request.getParameter("head_image");

        HashMap<String, String> settingData = new HashMap<>();

        if (nickname != null){
            if (!nickname.isEmpty()){
                Pattern pattern = Pattern.compile("([\u4e00-\u9fa5]|[a-z]|[A-Z]|[0-9]|-|_)+");
                Matcher nameValid = pattern.matcher(nickname);
                if (!nameValid.matches()){
                    out.println(JsonOut.error(201, "bad_nickname"));
                    return;
                }
                if ((new MysqlUtils()).isUsernameExist(nickname)){
                    out.println(JsonOut.error(113, "nickname_exist"));
                    return;
                }
                settingData.put("nickname", nickname);
            }

        }

        if (nickname != null){
            settingData.put("signature", signature);
        }

        if (area != null){
            settingData.put("area", area);
        }

        if (sex != null){
            if (sex.equals("0") || sex.equals("1") || sex.equals("2"))
                settingData.put("sex", sex);
        }

        if (birthday != null){
            settingData.put("birthday", birthday);
        }

//        if (headImage != null){
//            settingData.put("head_image", headImage);
//        }

        if (!(new MysqlUtils()).userSetInfo(email, settingData)){
            out.println(JsonOut.error(500, "sql_sth_wrong"));
            return;
        }

        out.println(JsonOut.error(0, "user_info_changed"));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println(JsonOut.error(403, "forbidden"));
    }
}
